version 16.1
clear all
cd "MYPATH\derived\integrate_kub_data"
adopath + ../../ado/
cap log close

preliminaries
log using "build.log", replace

foreach PATH in RESULTS TEMP {
	cap mkdir "${`PATH'}\derived"
	cap mkdir "${`PATH'}\derived\integrate_kub_data"
}

cap mkdir "${DATA}\KUB_data"

program main
	clean_full_data
end

program clean_full_data 
	* Load KUB update 
	use "MYPATH\MYPATH.dta" , clear
	count
	count if mi(undersokningsdatum)
	qui drop if mi(undersokningsdatum)
	
	* Merge in orig KUB data (so we take union of the two) 
	merge 1:1 lopnr undersokningsdatum foster_risk using "MYPATH\MYPATH.dta", ///
	  assert(1 2 3) keep(1 2 3) nogen
	
	* Clean variables
	qui {
		replace procedur = substr(procedur, 1, 1)
		destring procedur, replace
		gen did_amnio_kubdata = 1 if (procedur == 1)
		replace did_amnio_kubdata = 0 if inlist(procedur, 4, 5)
		gen did_cv_kubdata = 1 if (procedur == 2)
		replace did_cv_kubdata = 0 if inlist(procedur, 4, 5)
		label var did_amnio_kubdata "Did amnio? (KUB testing data)"
		label var did_cv_kubdata "Did cv? (KUB testing data)"
		drop procedur 
		gen chrom_ab_kubdata = 1 if !mi(kromosomer)
		rename utfall outcome_kubdata
		replace outcome_kubdata = substr(outcome_kubdata, 1, 1)
		destring outcome_kubdata, replace
		label var outcome_kubdata "Pregnancy outcome (KUB testing data)"
		label def lab_outcome 1 "Live birth" 2 "IUFD" 3 "post-natal death" 4 "abortion" ///
		  5 "missed ab (miscarriage)" 6 "miscarriage" 7 "mola (miscarriage)" 9 "unknown"
		label val outcome_kubdata lab_outcome
		label var kromosomer "Chromosomal Abnormalities (KUB testing data)"
	}
	save "${DATA}\KUB_data\cleaned_integrated_kub.dta", replace
end

* Execute * 
main
